home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / misc / gms_dev.lha / GMSDev / Source / C / 3DObjects / Helix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-07  |  4.2 KB  |  162 lines

  1. /* SAS/C: sc Helix.c opt math=standard INCDIR=INCLUDES:
  2. **
  3. ** This is a demo of a spinning helix consisting entirely of dots.  The
  4. ** object is pre-calculated then rotated in real time for speed, although
  5. ** things could be faster than this.  Move the mouse to scale the object
  6. ** to any size.
  7. */
  8.  
  9. #include <proto/dpkernel.h>
  10. #include <math.h>
  11.  
  12. BYTE *ProgName      = "Spinning Helix";
  13. BYTE *ProgAuthor    = "Paul Manias";
  14. BYTE *ProgDate      = "January 1998";
  15. BYTE *ProgCopyright = "DreamWorld Productions (c) 1996-1998.  Freely distributable.";
  16. BYTE *ProgShort     = "3-Dimensional object demonstration.";
  17.  
  18. struct GScreen *screen;
  19. struct JoyData *jport1;
  20.  
  21. void Demo(void);
  22.  
  23. struct DotPixel { double X,Y,Z; };
  24.  
  25. #define AMTCOLOURS 32
  26.  
  27. LONG palette[AMTCOLOURS+2] = {
  28.   PALETTE_ARRAY,32,
  29.   0x000000L,0x101010L,0x171717L,0x202020L,0x272727L,0x303030L,0x373737L,0x404040L,
  30.   0x474747L,0x505050L,0x575757L,0x606060L,0x676767L,0x707070L,0x777777L,0x808080L,
  31.   0x878787L,0x909090L,0x979797L,0xa0a0a0L,0xa7a7a7L,0xb0b0b0L,0xb7b7b7L,0xc0c0c0L,
  32.   0xc7c7c7L,0xd0d0d0L,0xd7d7d7L,0xe0e0e0L,0xe0e0e0L,0xf0f0f0L,0xf7f7f7L,0xffffffL
  33. };
  34.  
  35. /***********************************************************************************/
  36.  
  37. void main(void)
  38. {
  39.   if (screen = InitTags(NULL,
  40.      TAGS_SCREEN,    NULL,
  41.        GSA_BitmapTags, NULL,
  42.        BMA_Palette,    palette,
  43.        TAGEND,         NULL,
  44.      GSA_Attrib,     SCR_DBLBUFFER,
  45.      TAGEND)) {
  46.  
  47.      if (jport1 = Init(Get(ID_JOYDATA),NULL)) {
  48.         Display(screen);
  49.         Demo();
  50.  
  51.      Free(jport1);
  52.      }
  53.   Free(screen);
  54.   }
  55. }
  56.  
  57. /************************************************************************************
  58. ** Longtitude deterimines the position of the dot on the horizontal axis.
  59. ** Latitude determines the position of the dot on the vertical axis.
  60. */
  61.  
  62. #define AMTDOTS 500       /* The amount of dots in the Object. */
  63. #define MAXZ    6.3926    /* MAXZ  */
  64.  
  65. void Demo(void)
  66. {
  67.   struct DotPixel *object;
  68.   WORD   i;
  69.   WORD   offsetx = (screen->Width/2);
  70.   WORD   offsety = (screen->Height/2);
  71.   double temp;
  72.   double angle=0;
  73.   ULONG  colour;
  74.   double Z2,X2,Y2;
  75.   LONG   scale=16;
  76.   UWORD  anglex=0, angley=0, anglez=0;
  77.   double u,v;
  78.   double *sine;       /* Pointer to our sine table */
  79.   double *cosine;     /* Pointer to our cosine table */
  80.  
  81.   object = AllocMemBlock(sizeof(struct DotPixel)*AMTDOTS,MEM_DATA);
  82.   sine   = AllocMemBlock(sizeof(double)*360,MEM_DATA);
  83.   cosine = AllocMemBlock(sizeof(double)*360,MEM_DATA);
  84.  
  85.   /* First calculate the X, Y and Z coordinates of our object. */
  86.  
  87.   for (i=0; i<AMTDOTS; i++) {
  88.     u = ((double)FastRandom(12566-1)+1)/1000;  /*  0 < u < 4*PI  */
  89.     v = ((double)FastRandom(6283-1)+1)/1000;   /*  0 < v < 2*PI  */
  90.     object[i].X  = cos(u)*(2+cos(v));
  91.     object[i].Y  = sin(u)*(2+cos(v));
  92.     object[i].Z  = (u-2*3.14159)+sin(v);
  93.   }
  94.  
  95.   /* Now generate our cosine and sinus tables */
  96.  
  97.   for (i=0; i<360; i++) {
  98.     cosine[i] = cos(angle);
  99.     sine[i]   = sin(angle);
  100.     angle    += 0.25;
  101.   }
  102.  
  103.   /* Go into our main loop */
  104.  
  105.   do
  106.   {
  107.     Query(jport1);
  108.     scale += jport1->YChange;
  109.     if (scale < 1) scale = 1;
  110.     if (scale > 100) scale = 100;
  111.  
  112.     Clear(screen->Bitmap);
  113.  
  114.     for (i=0; i<AMTDOTS; i++) {
  115.  
  116.       X2 = object[i].X;
  117.       Y2 = object[i].Y;
  118.       Z2 = object[i].Z;
  119.  
  120.       /* Rotate the X axis */
  121.  
  122.       temp = Z2;
  123.       Z2 = Z2*cosine[anglex] - Y2*sine[anglex];
  124.       Y2 = Y2*cosine[anglex] + temp*sine[anglex];
  125.  
  126.       /* Rotate the Y axis */
  127.  
  128.       temp = Z2;
  129.       Z2 = Z2*cosine[angley] - X2*sine[angley];
  130.       X2 = X2*cosine[angley] + temp*sine[angley];
  131.  
  132.       /* Rotate the Z axis */
  133.  
  134. //      temp = X2;
  135. //      X2 = X2*cosine[anglez] - Y2*sine[anglez];
  136. //      Y2 = Y2*cosine[anglez] + temp*sine[anglez];
  137.  
  138.       /* Calculate colour based on Z position (-1.96 < Z < +1.96) */
  139.  
  140.       colour = (((Z2+MAXZ)/MAXZ)*screen->Bitmap->AmtColours)/2;
  141.  
  142.       /* Finally scale the (x,y) coordinates to enlarge or shrink the sphere */
  143.  
  144.       X2 *= scale;
  145.       Y2 *= scale;
  146.  
  147.       DrawPixel(screen->Bitmap,(WORD)X2+offsetx,(WORD)Y2+offsety,colour);
  148.     }
  149.     anglex++; if (anglex >= 360) anglex = 0;
  150.     angley++; if (angley >= 360) angley = 0;
  151.     anglez++; if (anglez >= 360) anglez = 0;
  152.  
  153.     WaitAVBL();
  154.     SwapBuffers(screen);
  155.   } while(!(jport1->Buttons & JD_LMB));
  156.  
  157.   FreeMemBlock(object);
  158.   FreeMemBlock(sine);
  159.   FreeMemBlock(cosine);
  160. }
  161.  
  162.